package C;

import java.util.Arrays;
import java.util.Scanner;

public class C23 {
    static int[] pizza;
    static long[][] cache;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        pizza = new int[n];
        for (int i=0;i<n;i++) {
            pizza[i] = sc.nextInt();
        }
        cache = new long[n][n];
        long ans = 0;

        for (int i=0;i<n;i++) {
            ans = Math.max(ans,recursive(check(i-1),check(i+1))+pizza[i]);
            System.out.println(ans);
        }
        System.out.println(ans);
        System.out.println(Arrays.deepToString(cache));

    }

    public static long recursive(int l,int r) {
        if (pizza[l]>pizza[r]) {
            l = check(l-1);
        } else {
            r = check(r+1);
        }

        if (cache[l][r]>0) {
            return cache[l][r];
        }

        if (r == l) {
            cache[l][r] = pizza[l];
            return pizza[l];
        } else {
            cache[l][r] = Math.max(recursive(check(l-1),r)+pizza[l],recursive(l,check(r+1))+pizza[r]);
        }
        return cache[l][r];
    }

    public static int check(int num) {
        if (num<0) {
            return pizza.length-1;
        } else if(num>=pizza.length) {
            return 0;
        } else {
            return num;
        }
    }
}
